//app.js
import Promise from './utils/es6-promise/es6-promise'
import regeneratorRuntime from './utils/regenerator-runtime/runtime'
const utils = require('./utils/util.js');

App({
  /* onLaunch: function () {
    //console.log(utils.regeneratorRuntime);
    await this.getToken();
      setTimeout(function(){
        console.log("onLaunch-delay");
      },2000)
      console.log("onLaunch");
  }, */
  data:{
    isHide:false,
    hideTimes:null
  },
  /* onShow:function(params){
    console.log("app-onShow");
    //var nowTimes=new Date().getTime();
    var hideTimes=this.data.hideTimes;
    var isHide=this.data.isHide;
    //if(!hideTimes || !isHide || nowTimes-hideTimes<60*1000*5) return;
    if(hideTimes && isHide){
      this.data.isHide=false;
      this.data.hideTimes=null;
      var path=params.path;
      var startUrl="/pages/index/index";
      var urls=["pages/index/index","pages/my/custom/submit/index"];
      if(urls.indexOf(path)<0){
        wx.redirectTo({
          url: startUrl
        })
      }
    }  
  }, */
  onShow: async function(){
    console.log("app-onshow");
    var isHide=this.data.isHide;
    if(!isHide) return;
    this.data.isHide=false;
    wx.showLoading({
      title:"加载中..."
    })
    var token=wx.getStorageSync('token');
    if(token) {
      const sessionEnable = await this._checkSession();
      if(!sessionEnable) this._jumpIndex();
    }
    else this._jumpIndex();
    var data=await this.getLeftFormId(token); 
    if(data.state){
      var data=data.data;
      if(data.usertype!=this.globalData.usertype) this._jumpIndex();
    }
    else this._jumpIndex();
    wx.hideLoading();
  },
  onHide:function(){
    console.log("app-onHide");
    this.data.isHide=true;
    this.data.hideTimes=new Date().getTime();   
  },
_jumpIndex:function(){
  wx.hideLoading();
  wx.redirectTo({
    url:"/pages/index/index"
  })
  return;
},
_checkSession:function(){
    var p = new Promise((resolve,reject)=>{
        //做一些异步操作
        wx.checkSession({
    　　　　success: function(res){
    　　　　　　console.log("处于登录态");
                resolve(true);
    　　　　},
    　　　　fail: function(res){
              console.log("需要重新登录");
              resolve(false)
    　　　　}
    　　})
    });
    return p;
},
_login:function(){
  var p = new Promise((resolve,reject)=>{
    //做一些异步操作
    wx.login({
　　　　success: function(res){
          resolve(res.code);
　　　　},
      fail:function(res){
        resolve(null);
      }
　　})
  });
  return p;
},
_doLogin:function(code,res_code){
  return new Promise((resolve,reject)=>{
    //做一些异步操作
    this.request("/api/login/dologin",{code:code,res_code:res_code},function(res){
        wx.setStorageSync('token', res.data.token)
        resolve(res.data.token);
    },function(){
      resolve(null);
    });
  });
  
},
getLeftFormId:function(token){
  var p = new Promise((resolve,reject)=>{
      //做一些异步操作
      this.request("/api/message/getLeftFormid",{token:token},res=>{
        console.log(res);
        resolve({state:res.data.state,data:res.data.data});
      })
  });
  return p;
},
getToken: async function(isFresh,res_code){
    res_code=res_code || "";
    var token=isFresh?null:wx.getStorageSync('token');
    if(token) {
      const sessionEnable = await this._checkSession();
      if(!sessionEnable){
        const code = await this._login();
        if(code==null) return null;
        console.log("_login");
        const newToken = await this._doLogin(code,res_code);
        if(newToken==null) return null;
        console.log("_doLogin");
        return newToken;
      }
      else return token;
    }
    else{
      const code = await this._login();
      if(code==null) return null;
      console.log("_login2");
      const newToken = await this._doLogin(code,res_code);
      if(newToken==null) return null;
      console.log("_doLogin2");
      return newToken;
    }
  },

  request:function(url,data,success,fail,complete){
    wx.request({
      url: this.globalData.baseUrl+url,
      data: data,
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success:function(res){
        var data=res.data;
        //debugger;
        console.log("request===");
        console.log(data);
        if((data.state==false || data.state=="fasle") && (data.msg=="登录已过期" || data.msg=="登陆已过期")){
          wx.redirectTo({
            url: "/pages/index/index"
          })
        }
        else success(res);
      },
      fail:fail,
      complete:complete
    })
  },
  globalData: {
    userInfo: null,
    /* appid:"wx7d57caa1a1023d37",
    appsecret: "e9d90a1126080f4e0156544d9b43ea64", */
    globalDataFormIds:null,
    basePath:"https://bkxcx.fumeihufu.com",
    baseUrl:"https://bkxcx.fumeihufu.com/index.php",
    token:null,
    baiduAppKey:"NEmCGu6HBjC2GBmeSA9ZXNsUXqL4wSgm",
    cityname:null
  }
})